<div class="span3" id="account-add-block">
	<h4 class="title"><?php echo t('page', 'Add WHM Account'); ?></h4>
	<div class="squiggly-border"></div>
	<div class="row-fluid">
		<div class="span12 add_account_form_info hide" id="auth_account_success_info">
			<div class="alert alter-block alert-success">
	<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
				<h4>Success</h4>
				<div class="content">Successfully Authenticated, Please wait while we download your cPanel account data.</div>
			</div>
		</div>
	</div>
	<div class="row-fluid">
		<div class="span12 add_account_form_info hide" id="add_account_success_info">
			<div class="alert alter-block alert-success">
	<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
				<h4>Success</h4>
				<div class="content">New account has been added!</div>
			</div>
		</div>
	</div>
	<div class="row-fluid">
		<div class="span12 add_account_form_info hide" id="add_account_error_info">
			<div class="alert alter-block alert-error">
	<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
				<h4>Error</h4>
				<div class="content"></div>
			</div>
		</div>
	</div>
	
	<div class="row-fluid">
		<div class="span12 add_account_form_info hide" id="account_limit_warning">
			<div class="alert alter-block alert-error">
				<h4>Warning</h4>
				<div class="content">
					<?php if(!$whmaccount->checkAccountsLimit()): ?>
						Maximum WHM Account limit has been reached.
					<?php elseif($whmaccount->checkAccountsLimitWarning()): ?>
						You have exceeded 90% of your WHM Account limit.					
					<?php endif; ?>
				</div>
			</div>
		</div>
		<script>
			if(trim($('#account_limit_warning .content').text()) != ''){
				$('#account_limit_warning').show();
			}
		</script>
	</div>
	
	<?php $form = $this->beginWidget('CActiveForm', array(
		'id' => 'account-add-form',
		'enableClientValidation' => true,
		'clientOptions' => array(
			'validateOnSubmit' => true,
		),
	)); ?>
	
		<div class="row-fluid">
			<?php echo $form->textField($whmaccountAddForm, 'domain', array('placeholder' => $whmaccountAddForm->getFieldLabel('domain'), 'class' => 'fields')); ?>
		</div>
	
		<div class="row-fluid">
			<?php echo $form->textField($whmaccountAddForm, 'ipaddress', array('placeholder' => $whmaccountAddForm->getFieldLabel('ipaddress'), 'class' => 'fields')); ?>
		</div>
		
		<div class="row-fluid">
			<?php echo $form->dropDownList($whmaccountAddForm, 'id_group', $whmaccountAddForm->listExistedGroups(), array('class' => 'fields', 'empty' => 'Select Group')); ?>
		</div>
		
		<div class="row-fluid">
			<?php echo $form->dropDownList($whmaccountAddForm, 'id_host', $whmaccountAddForm->listExistedHosts(), array('class' => 'fields', 'empty' => 'Select Host')); ?>
		</div>
		
		<div class="row-fluid">
			<?php echo $form->textField($whmaccountAddForm, 'username', array('placeholder' => $whmaccountAddForm->getFieldLabel('username'), 'class' => 'fields')); ?>
		</div>
		
		<div class="row-fluid">
			<?php echo $form->passwordField($whmaccountAddForm, 'pass', array('placeholder' => $whmaccountAddForm->getFieldLabel('pass'), 'class' => 'fields')); ?>
		</div>
		
		<div class="row-fluid">
			<?php echo $form->textArea($whmaccountAddForm, 'accesshash', array('placeholder' => $whmaccountAddForm->getFieldLabel('accesshash'), 'class' => 'fields')); ?>
		</div>
	
		<div class="row-fluid buttons">
			<?php echo CHtml::ajaxSubmitButton(
				t('buttons', 'Add WHM Account'), 
				app()->createUrl('whm/account/auth'),
				array(
					'type' => 'post',
					'beforeSend' => 'function(){
						$("#add_account_submit").button("loading");
					}',
					'success' => 'function(ret){
						$(".add_account_form_info").hide();
						$("#add_account_submit").button("reset");
						
						if(ret["result"]){
							$("#auth_account_success_info").blink({
								"callBack": function(){
									$("#add_account_submit").button("loading");
									$.ajax({
										type: "post",
										url: "'.url('whm/account/index').'",
										data: $("#account-add-form").serialize(),
										success: function(ret){
											$("#add_account_submit").button("reset");
											if(ret["result"]){
												$("#add_account_success_info").blink({
													"beforeCall": function(){
														var tr = $("<tr class=\"new-line\" />");
														tr.append( $("<td class=\"hide id_account\" />").html(ret["detail"]["id_account"]) );
														tr.append( $("<td />").html('.json_encode(CHtml::checkBox("accounts[]", false, array("class" => "accounts_check"))).') );
														tr.append( $("<td class=\"username\" />").html(ret["detail"]["username"]) );
														tr.append( $("<td class=\"domain\" />").html(ret["detail"]["domain"]) );
														tr.append( $("<td class=\"ipaddress\" />").html(ret["detail"]["ipaddress"]) );
														tr.append( $("<td class=\"group\" />").html($("#WhmAccountAddForm_id_group option:selected").text().replace("Group: ", "")) );
														tr.append( $("<td class=\"host\" />").html($("#WhmAccountAddForm_id_host option:selected").text().replace("Host: ", "")) );
														tr.append( $("<td />").html('.json_encode(WhmAccount::model()->renderListActionButtons()).') );
														
														$("#account_grid table").prepend(tr);
														if(ret.detail.pass != ""){
															$(".new-line .account_whm_login").removeClass("hide");
														}
														
														$(".new-line").removeClass("new-line");
														$("form#account-add-form .fields").val("");
													}
												});
											}else{
												$("#add_account_error_info").blink({
													"beforeCall": function(){
														$("#add_account_error_info .content").html("");
														$.each(ret["errors"], function(attrName, error){
															$(error).each(function(k, e){
																var p = $("<p />").addClass("text-error").append(attrName+": "+e);
																$("#add_account_error_info .content").append(p);
															});
														});
													}
												});
											}
										},
										dataType: "json"
									});
								}
							});
						}else{
							$("#add_account_error_info").blink({
								"beforeCall": function(){
									$("#add_account_error_info .content").html("");
									$.each(ret["errors"], function(attrName, error){
										$(error).each(function(k, e){
											var p = $("<p />").addClass("text-error").append(attrName+": "+e);
											$("#add_account_error_info .content").append(p);
										});
									});
								}
							});
						}
					}',
					'dataType' => 'json'
				),
				array(
					'id' => 'add_account_submit',
					'type' => 'button', 
					'class' => 'btn btn-small btn-primary',
					'data-loading-text' => t('buttons', 'Saving...')
				)
			); ?>
		</div>
	
	<?php $this->endWidget(); ?>
</div>